$("#get-token-btn").click(function () {
  var formData = new FormData();
  formData.append("grant_type", "authorization_code");
  formData.append(
    "redirect_uri",
    "http://127.0.0.1:8080/login/oauth2/code/oidc-client"
  );
  var authorization_code_val = $("#show-code").text();
  if (!authorization_code_val) {
    throw new Error("authorization_code is null!");
  }
  console.log(authorization_code_val);
  formData.append("code", authorization_code_val);
  formData.append(
    "code_verifier",
    "z1W46zuLHiQbb3fu77xz7ZPWiFAn3cl6GHC70SYZwgY"
  );
  $.ajax({
    url: "/oauth2/token",
    //url: "/client-form-submit",
    type: "POST",
    cache: false,
    contentType: false, // 重要：不要在这里设置contentType
    processData: false,
    data: formData,
    beforeSend: function (xhr) {
      // jquery中不要写content-type会自动处理
      xhr.setRequestHeader(
        "Authorization",
        "Basic " + btoa("oidc-client:secret")
      );
    },
    success: function (response) {
      console.log("authorization_code_btn:", response);
      // 将 JavaScript 对象转换为格式化后的 JSON 字符串
      console.log(response.access_token);
      var formattedJsonString = JSON.stringify(response, null, 2);
      $("#show-token").html("<pre>"+formattedJsonString+"</pre>");
    },
    error: function (xhr, status, error) {
      console.error("Error:", error);
    },
  });
});
